﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using mscook.mvc.Filters;
using mscook.mvc.Models;
using mscook.core.services;
using mscook.mvc.Core;
using mscook.core.domain;
using System.Security.Principal;
using mscook.mvc.core;
namespace mscook.mvc.Controllers
{
    public class MaterialController : BaseController
    {
        //
        // GET: /Material/
        private ILoginUserService iLoginUserService;
        private IMaterialService iMaterialService;
        public MaterialController(IActionBonusSettingService actionBonusSettingService, IResourceBonusSettingService resourceBonusSettingService, ILoginUserService loginUserService, IMaterialService iMaterialService)
            : base(actionBonusSettingService, resourceBonusSettingService, loginUserService)
        {
            this.iLoginUserService = loginUserService;
            this.iMaterialService = iMaterialService;
        }
        public ActionResult Index(string[] resourceTypes, string num = "", string keyword = "")
        {
            MaterialModel materialModel = new MaterialModel();
            long total = 0;
            PageBreakHelper pageBreakHelper = new PageBreakHelper();
            pageBreakHelper.Init(num, "12");
            // materialModel.MaterialList = iMaterialService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            if (resourceTypes != null)
            {
                IList<string> resoureTypeList = resourceTypes.Where(o => !string.IsNullOrEmpty(o)).ToList();
                //cookBookModel.CookBookList = iCookBookService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                if (resoureTypeList.Count == 0)
                {
                    materialModel.MaterialList = GetByKeywordOrPage(pageBreakHelper, keyword, out total);
                }
                else
                {
                    materialModel.MaterialList = iMaterialService.SelectLimitbyIds(GetRsourceIdsByTypes(resoureTypeList, PurchasedCategory.Material), pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                    materialModel.SelectResourceTypes = resoureTypeList;
                }
            }
            else
            {
               materialModel.MaterialList= GetByKeywordOrPage(pageBreakHelper,keyword,out total);
            }
            pageBreakHelper.SetViewDataKeys(ViewData, ViewBag, total);
            InitSourceType(materialModel, PurchasedCategory.Material);
            InitNavBar();
            return View(materialModel);
        }
        [ResourceBonusFilter]
        [ActionBonusFilter]
        public ActionResult ReadMaterial(string id)
        {
            Material material = iMaterialService.Get(id);
            if (material != null)
            {
                InitNavBar();
                ReadMaterialModel readMaterialModel = new ReadMaterialModel() { Material = material };
                readMaterialModel.Magazine = GetPushMagazine();
                readMaterialModel.LoginUser = CurrentUser;
                return View(readMaterialModel);
            }

            return RedirectToAction("Index");
        }


        private IList<Material> GetByKeywordOrPage(PageBreakHelper pageBreakHelper, string keyword, out long total)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return iMaterialService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            }
            else
            {
                return iMaterialService.GetByKeyword(keyword, pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            }
        }

    }
}
